﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MLM.Data;

namespace MLM.AdminCP.Class
{
    public static class NumberUtil
    {
        public static string Round(object number)
        {
            if (number != null)
            {
                if (number.ToString().Contains(","))
                {
                    if (number.ToString().Split(new char[] { ',' })[1].Length == 2)
                    {
                        return number.ToString();
                    }
                }
                else if(number.ToString().Contains("."))
                {
                    if (number.ToString().Split(new char[] { '.' })[1].Length == 2)
                    {
                        return number.ToString();
                    }
                }
                var returnValue = Math.Round(decimal.Parse(number.ToString()), 2).ToString();
                try
                {
                    if (returnValue.Contains(","))
                    {                       
                        if (returnValue.Split(new char[] { ',' })[1].Length == 0)
                        {
                            returnValue += "00";
                        }
                        else if (returnValue.Split(new char[] { ',' })[1].Length == 1)
                        {
                            returnValue += "0";
                        }
                    }
                    else if(returnValue.Contains("."))
                    {
                        if (returnValue.Split(new char[] { '.' })[1].Length == 0)
                        {
                            returnValue += "00";
                        }
                        else if (returnValue.Split(new char[] { '.' })[1].Length == 1)
                        {
                            returnValue += "0";
                        }
                    }
                    else
                    {
                        returnValue += ",00";
                    }
                    returnValue = returnValue.Replace(".", ",");
                    return returnValue;
                }
                catch (Exception)
                {
                    return returnValue;
                }
            }
            else
            {
                return "";
            }
        }
    }
    public static class PriceUtil
    {
        public static decimal FormatPrice(object price, object tax, MLMDataContext ct, int? role, object productid = null, object countryid = null)
        {
            //-1 = auto get tax
            try
            {
                decimal pValue = 0;
                if (productid != null && countryid != null && countryid != "" && productid != "")
                {

                    long pId = long.Parse(productid.ToString());
                    long cId = long.Parse(countryid.ToString());
                    var pPrice =
                        ct.ProductPrices.FirstOrDefault(
                            it => it.ProductBaseID == pId && it.CountryID == cId && it.RoleID == role);
                    double? newtax = 0;
                    try
                    {
                        if (decimal.Parse(tax.ToString()) == -1)
                        {
                            var taxtitem = ct.GroupTaxes.FirstOrDefault(it => it.CountryID == cId && it.RoleID == role);
                            if (taxtitem != null)
                            {
                                newtax = taxtitem.VAT ?? 0;
                                if (newtax == 0)
                                {
                                    newtax = taxtitem.VATSpecial ?? 0;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        newtax = 0;
                    }
                    if (pPrice != null)
                    {
                        decimal _price = decimal.Parse(pPrice.Price.ToString());
                        decimal _tax = decimal.Parse(newtax.ToString());
                        pValue = (_price + _price * (_tax / 100));
                    }
                    else
                    {
                        decimal _price = decimal.Parse(price.ToString());
                        decimal _tax = decimal.Parse(newtax.ToString());
                        pValue = _price + _price * (_tax / 100);
                    }
                }
                return pValue;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static int? GetRole(MLMDataContext ct, object _userId)
        {
            long userId = long.Parse(_userId.ToString());
            return ct.Users.FirstOrDefault(it => it.UserId == userId).RoleID;
        }
        public static long? GetCountry(MLMDataContext ct, object _userId)
        {
            long userId = long.Parse(_userId.ToString());
            return ct.Users.FirstOrDefault(it => it.UserId == userId).CountryID;
        }
    }
}